<?php
/**
 *user Management Controller
 *
 *@author S.Feil, T.Ziegler, M.El-Hindi <goatymail@googlemail.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, S.Feil, T.Ziegler, M.El-Hindi
 *@package cms
 *
 */
/**
 * Class userMgmt_Controller
 * Administers the Users of the cms
 *@package controller
 */
class userMgmt_Controller extends Controller{



    /**
     * Stores the user model
     * @var object
     */
    private $oUserModel;

    /**
     * Language in wich to show page
     * @var integer
     */
    private $iLang ='';

    /**
     * Stores the template
     * @var string
     */
    private $sTemplate ='productCategories';

    /**
     * Reference to model that retrieves page data from db
     * @var object
     */
    private $oContentModel;

    /**
     * Reference to view of content section
     * @var object
     */
    private $oContentView;

    /**
     * Reference to model that retrieves page navigation elements from db
     * @var object
     */
    private $oNavigationModel;

    /**
     * Reference to view of navigation section
     * @var object
     */
    private $oNavigationView;

    /**
     * Reference to view of left navigation section in cms, it shows the entire pagestructure
     * @var object
     */
    private $oLeftNavigationView;

    /**
     * Error message which will be displayed in case of error
     * @var string
     */
    private $sError;

    public function __construct(){
        //Gets Viewmanger reference from parent constructor
        parent::__construct();
        //Loads models and views
        $this->oUserModel = Loader::loadModel('userMgmt');
        $this->oNavigationModel = Loader::loadModel('navigation');
        $this->oContentView = $this->oViewmanager->getView('userMgmt');
        $this->oNavigationView = $this->oViewmanager->getView('navigation');
        $this->oLeftNavigationView = $this->oViewmanager->getView('leftnavigation');

        //Checks language set in Cookie, take default language specified in config if not set
        if (isset($_COOKIE['language'])){
            $this->iLang = intval($_COOKIE['language']);
        }else{
            $this->iLang = "1";
        }
    }

    public function checkArgs($aArgs){
        //Reads out the the method which should be called
        $sArg = array_shift($aArgs);

        // ChangeUsername
        if($sArg == 'changeUsername'){
            if($_POST['username'] != ""){
                $sNewUsername = $_POST['username'];
                $sOldUsername = $_SESSION['username'];
                //Changes the username
                if($this->oUserModel->changeUsername($sOldUsername, $sNewUsername)){
                    $_SESSION['username'] = $sNewUsername;
                    $this->sError = "Ihr Username wurde erfolgreich ge&auml;ndert. Bitte verwenden Sie ihn beim n&auml;chsten Login!";
                }else{
                    $this->sError = "Es ist ein Fehler im Model aufgetreten!";
                }
            }else{
                $this->sError = "Es wurde keine neuer Username angegeben";
            }
            //ChangeMail
        }elseif($sArg == 'changeEmail'){
            if($_POST['email'] != ""){
                $sNewEmail = $_POST['email'];
                $sUsername = $_SESSION['username'];
                //Changes the email adress
                if($this->oUserModel->changeEmail($sUsername, $sNewEmail)){
                    $this->sError = "Ihre E-Mail Adresse wurde erfolgreich ge&auml;ndert";
                }else{
                    $this->sError = "Es ist ein Fehler im Model aufgetreten!";
                }
            }else{
                $this->sError = "Bitte geben Sie in das Feld eine neue E-Mail Adresse ein";
            }
            //ChangePwd changes the password
        }elseif($sArg == 'changePwd'){
            $sUsername = $_SESSION['username'];
            $sNewPassword = "";
            $sNewPasswordRe = "";
            $sOldPassword = "";
            //Validates the password and the repeation
            if ($_POST['newPassword'] != ""){
                $sNewPassword = $_POST['newPassword'];
                if($_POST['newPasswordRe'] != ""){
                    $sNewPasswordRe = $_POST['newPasswordRe'];
                    if($sNewPassword == $sNewPasswordRe){
                        if($_POST['oldPassword'] != ""){
                            $sOldPassword = $this->passwordHash($_POST['oldPassword']);
                            //Checks the old password
                            if($this->oUserModel->checkPwd($sUsername, $sOldPassword)){
                                $sNewPassword = $this->passwordHash($sNewPassword);
                                //Changes password
                                if($this->oUserModel->changePwd($sUsername, $sNewPassword)){
                                    $this->sError = "Ihr Passwort wurde erfolgreich ge&auml;ndert.Bitte verwenden Sie es beim n&auml;chsten Login!";
                                }else{
                                    $this->sError = "Es ist ein Fehler im Model aufgetreten!";
                                }
                            }else{
                                $this->sError = "Das alte Passwort ist falsch!";
                            }
                        }
                    }else{
                        $this->sError = "Die Wiederholung des Passworts war nicht korrekt, bitte wiederholen Sie Ihre Eingabe!";
                    }
                }else{
                    $this->sError = "Sie müssen das neue Passwort wiederholen!";
                }

            }else{
                $this->sError = "Bitte ein neues Passwort eingeben!";
            }
        }
    }
    /**
     * Executes controller methods
     * @param array different arguments
     */
    public function execute($aArgs){
        //Checks the arguments in the method checkArgs
        $this->checkArgs($aArgs);

        //Passes data to view
        $this->oContentView->assign('content', $this->oUserModel->getUserInfos($_SESSION['username']));
        $this->oContentView->assign('error', $this->sError);
        $this->oNavigationView->assign('contentANDproductPages', $this->oNavigationModel->getPageStructure('NULL', $this->iLang, "(type = 'contentPage' OR type = 'product')"));
        $this->oNavigationView->assign('products', $this->oNavigationModel->getPagesByTyp('', $this->iLang, 'product'));
        $this->oNavigationView->assign('categories', $this->oNavigationModel->getPagesByTyp('', $this->iLang, 'category'));
        $this->oLeftNavigationView->assign('leftnavigation', $this->oNavigationModel->getPageStructure('NULL', $this->iLang));
        //Inserts data in the correct sections of the website layout
        $this->oViewmanager->setSection('Title', $this->oUserModel->getPageTitle());
        $this->oViewmanager->setSection('Content', $this->oContentView->display());
        $this->oViewmanager->setSection('LeftNavigation', $this->oLeftNavigationView->display());
        $this->oViewmanager->setSection('Navigation', $this->oNavigationView->display());
    }
    // Hashes the password with sha256 and the salt
    private function passwordHash($sPassword){
        $sPassword = str_split($sPassword,(strlen($sPassword)/2));
        $sPassword = hash('sha256', $sPassword[0].'Cz]#%1K|'.$sPassword[1]);
        return $sPassword;
    }
}

?>